#!/bin/bash
#----------------------------
#   export LANG=zh_CN.utf8
#   export LC_CTYPE=zh_CN.utf8
#   export LANGUAGE=zh_CN.utf8
#   export LC_TIME=zh_CN.utf8
#----------------------------
export XAUTHORITY="/tmp/${USER}.Xauthority"
if [ -e ~/.Xmodmap ] ; then
    xmodmap ~/.Xmodmap
fi
if [ -e ~/.xmodmap ] ; then
    xmodmap ~/.xmodmap
fi

if [ -e /proc/acpi/ibm/hotkey ] ; then
    export USE_LAPTOP=1
else
    export USE_LAPTOP=0
fi
export USE_XFCE4_PANEL=0
#----------------------------
#----------------------------
export LANGUAGE="zh_CN:zh:en_US:en"
export LC_CTYPE="zh_CN.UTF-8"
export LANG="zh_CN.UTF-8"
export LC_TIME="zh_CN.UTF-8"
#----------------------------

export PATH=${PATH}:/usr/kde/4.2/bin

#----------------------------
#=======================================
#export XMODIFIERS="@im=fcitx"
#export XIM=fcitx
#export XIM_PROGRAM=fcitx
#=======================================
# Open office fix
export OOO_FORCE_DESKTOP=gnome 
#export OOO_FORCE_DESKTOP=kde


#----------------------------
#scim -f socket -c socket -d
#----------------------------
#   if [ `pidof urxvtd` ] ; then
#       killall urxvtd >/dev/null 2>&1
#   fi
#
#   if [ `pidof fcitx` ] ; then
#       killall fcitx  >/dev/null 2>&1
#   fi
#
#   #killall rox
#   #exec urxvtd&
#   exec fcitx&

if [ `pidof urxvtd|wc -w` -eq 0 ] ; then
    exec urxvtd&
else
    killall urxvtd
    exec urxvtd&
fi

if which dbus-launch >/dev/null && test -z "$DBUS_SESSION_BUS_ADDRESS"; then
           eval "$(dbus-launch --sh-syntax --exit-with-session)"
fi

#   if [ `pidof fcitx|wc -w` -eq 0 ] ; then
#       exec fcitx&
#   else
#       killall fcitx
#       exec fcitx&
#   fi

default_wm=xmonad
default_wm=musca
default_wm=og
default_wm=ok
default_wm=awesome
default_wm=lxde
default_wm=openbox

#   if [ -z $mywm ] ; then
#       if [ -z $1 ] ; then
#           start_wm=$default_wm
#       else 
#           start_wm=$1
#       fi
#   else
#       default_wm=$mywm
#   fi
start_wm=${mywm:-$default_wm}

#defaultwm=ob   # 指定如果没有提供启动 wm 参数，默认为启动 KDE。
windowmgr=${1:-$defaultwm}
windowmgr=${1:-$start_wm}
#根据参数不同启动不同的 wm
case ${windowmgr} in
    kde|kwm|kdestart)
    #WINDOWMANAGER=startkde
    WINDOWMANAGER=openbox-kde-session
    ;;
    flux|fluxbox)
    WINDOWMANAGER=fluxbox
    #exec spaceclock &  # 这里我加入了两个程序随 fluxbox 启动
    #exec xidesk &
    ;;
    gnome)
    WINDOWMANAGER=gnome-session
    ;;
    xfce4|xfce)
    WINDOWMANAGER=startxfce4
    #killall xfwm4 ; openbox & exit
    #xfdesktop --quit
    ;;
    ok|openbox-kde)
    WINDOWMANAGER=openbox-kde-session
    ;;
    og|openbox-gnome)
    WINDOWMANAGER=openbox-gnome-session
    ;;
    ob|openbox)
    WINDOWMANAGER=openbox-session
    ;;
    ice|icewm)
    WINDOWMANAGER=icewm-session
    ;;
    awe|awesome)
        WINDOWMANAGER=awesome
    ;;
    pek|pekwm)
        WINDOWMANAGER=pekwm
    ;;

    xmon|xmonad)
        WINDOWMANAGER=xmonad
        if [ -e ~/.Xmodmap ] ; then
            xmodmap ~/.Xmodmap
        fi
        if [ -e ~/.xmodmap ] ; then
            xmodmap ~/.xmodmap
        fi
    ;;
    mu|musca)
        WINDOWMANAGER=musca
        if [ -e ~/.Xmodmap ] ; then
            xmodmap ~/.Xmodmap
        fi
        if [ -e ~/.xmodmap ] ; then
            xmodmap ~/.xmodmap
        fi
    ;;
    lxde)
        WINDOWMANAGER=startlxde
    ;;

    *) WINDOWMANAGER=$windowmgr
esac

#----------------------------
#scim -d
#exec rox -p default &
#exec fbpanel &
#exec /usr/bin/fvwm-crystal
#exec gnome-session
#exec xfce4-session
#exec icewm-session
#exec /usr/bin/fvwm
#exec /usr/bin/openbox
#==========================
#exec openbox-session
#==========================
#exec dwm
#exec openbox-gnome-session
#exec /usr/bin/wmaker
#exec startfluxbox
#exec /usr/bin/fluxbox
#exec /usr/bin/fvwm-themes-start
#exec /usr/bin/icewm-session
#----------------------------
echo "wm=$WINDOWMANAGER"
awesome2_startup()
{
    export OOO_FORCE_DESKTOP=gnome
    if [ -e ~/.Xmodmap ] ; then
        xmodmap ~/.Xmodmap
    fi
    if [ -e ~/.xmodmap ] ; then
        xmodmap ~/.xmodmap
    fi

    # Xresources
    if [ -e ~/.Xresources ] ; then
        xrdb -merge ~/.Xresources
    fi 


    if [ `pidof trayer|wc -w` -eq 0 ] ; then
        :
    else 
       killall trayer
   fi
    trayer --edge right --widthtype request --width 200 --SetDockType true --transparent true --expand false&
    
    if [ `pidof awesome-diskusage|wc -w` -eq 0 ] ; then
        :
    else
        killall awesome-clock
    fi
    exec ~/MultiConfigDir/awesome/awesome-diskusage &


    if [ `pidof awesome-clock|wc -w` -eq 0 ] ; then
        :
    else
        killall awesome-clock
    fi
    exec ~/MultiConfigDir/awesome/awesome-clock&
    ## 用 gnome-volume-manager 来自动挂载 U 盘等
    gnome-volume-manager &
    gnome-power-manager &
    gnome-screensaver &
#关闭触摸块
#x11-drivers/synaptics (/usr/bin/synclient)
echo "synclient TouchpadOff=1"
synclient TouchpadOff=1 &
#-------------------------
xset b off  #terminal bell off
#-------------------------


#----------------------------
export LANGUAGE="zh_CN:zh:en_US:en"
export LC_CTYPE="zh_CN.UTF-8"
export LANG="zh_CN.UTF-8"
export LC_TIME="zh_CN.UTF-8"
#----------------------------
#
#----------------------------
#scim -f socket -c socket -d
#----------------------------
#
if [ `pidof urxvtd|wc -w` -eq 0 ] ; then
    exec urxvtd&
#else
#    killall urxvtd
#    exec urxvtd&
fi

#   if [ `pidof fcitx|wc -w` -eq 0 ] ; then
#       exec fcitx&
#   #else
#   #    killall fcitx
#   #    exec fcitx&
#   fi
#
#
}



#nvidia-settings -a InitialPixmapPlacement=2 -a GlyphCache=1
#echo "nvidia-settings -a InitialPixmapPlacement=2 -a GlyphCache=1"
awesome_ver=3
if [ $WINDOWMANAGER == "awesome" ] ; then
    if [ $awesome_ver -eq 3 ] ; then
        if [ -e ~/.config/awesome/scripts/awesome_startup.sh ] ; then
            #source ~/.config/awesome/scripts/awe_autostart.sh
            source ~/.config/awesome/scripts/awesome_startup.sh 
            awesome3_startup
        fi
    else
        echo "awesome2_startup"
        awesome2_startup
    fi
    #exec $WINDOWMANAGER >~/log_awesome_run_err.log 2>&1
    #exec $WINDOWMANAGER 2>~/log_awesome_run_err.log
    exec $WINDOWMANAGER 
else
    exec $WINDOWMANAGER
fi
#if [ $WINDOWMANAGER == "pekwm" ] ; then
    #exec ~/.pekwm/autostart.sh
#fi

#exec $WINDOWMANAGER

killall urxvtd >/dev/null 2>&1
killall fcitx >/dev/null 2>&1
killall fbpanel >/dev/null 2>&1
killall rox  >/dev/null 2>&1
#----------------------------
#eval `dbus-launch --exit-with-session --sh-syntax`
